package net.borlis.sample.services.configuration;

import net.borlis.configuration.common.ApplicationConfiguration;
import net.borlis.configuration.context.apache.ConfigurationData;
import net.borlis.configuration.context.apache.ConfigurationStorage;
import net.borlis.configuration.properties.PropertiesData;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net on 25.07.2014.
 */
@ApplicationScoped
public class ServiceContextProvider {

    @Inject
    private Logger logger;

    @Inject @PropertiesData
    private ApplicationConfiguration config;

    @PostConstruct
    public void initContext() {
        logger.info("emulate the context for the tests");
        ConfigurationData data = new ConfigurationData();
        data.setSchemaVersion(config.getSchemaVersion());
        data.setDatabaseDriver(config.getDriverName());
        data.setDatabaseServer(config.getServer());
        data.setDatabasePort(config.getPort());
        data.setDatabaseUser(config.getJdbcUser());
        data.setDatabasePassword(config.getJdbcPassword());
        ConfigurationStorage.setData(data);
        logger.info(data + " has been emulated for the context for the tests");
    }

    public String showStatus(){
        logger.info("the context emulated");
        return "THE CONTEXT IS READY";
    }

}
